package com.hwg.emp.common.service;

import java.util.Collection;

import com.hwg.emp.common.exception.EmpException;


public abstract class CommonService {
	
	protected void assertNull(String message,Object obj) {
		if(obj!=null)throw new EmpException(message);
	}
	protected void assertNotNull(String message,Object obj) {
		if(obj==null)throw new EmpException(message);
	}
	/**
	 * 断言字符串是null或者空字符串或空白字符串
	 * @param message
	 * @param s
	 * @param args
	 */
	protected void assertBlank(String message,String s) {
		if(s!=null&&s.trim().length()!=0){
				throw new EmpException(message);		
		}
	}
	/**
	 * assertNotBlank便捷方法
	 * @param message
	 * @param s
	 * @param args
	 * @return
	 */
	protected String $(String message,String s) {
		return this.assertNotBlank(message, s);
	}
	
	
	protected String assertNotBlank(String message,String s) {
		if(s==null||(s=s.trim()).length()==0){
				throw new EmpException(message);		
		}
		return s;
	}
	protected int assertInteger(String message,String intStr) {
		if(intStr==null||intStr.trim().length()==0){
			throw new EmpException(message);
		}
		try{
			return Integer.parseInt(intStr);
		}catch(Exception e){
			throw new EmpException(message);
		}
	}
	protected void assertNotEquals(String message,Object a,Object b) {
		if(a==null?a==b:a.equals(b)){
			throw new EmpException(message);
		}
	}
	protected void assertEquals(String message,Object a,Object b) {
		if(a==null?a!=b:!a.equals(b)){
			throw new EmpException(message);
		}
	}
	protected Collection assertEmpty(String message,Collection b) {
		if(b==null||b.size()!=0){
			throw new EmpException(message);
		}
		return b;
	}
	protected Collection assertNotEmpty(String message,Collection b) {
		if(b!=null&&b.size()==0){
			throw new EmpException(message);
		}
		return b;
	}
	protected void assertTrue(String message,boolean b) {
		if(!b){
			throw new EmpException(message);
		}
	}
	protected void assertFalse(String message,boolean b) {
		if(b){
			throw new EmpException(message);
		}
	}
	protected String ifBlank(String s,String def){
		if(s==null||(s=s.trim()).length()==0){
			return def;
		}
		return s;
	}
	
	/**
	 * 验证对象是否为空
	 * @param t 对象
	 * @param def 对象的默认值
	 * @return 如果对象为空 则返回默认值 否则返回对象
	 */
	protected <T>T ifNull(T t,T def){
		if(t==null){
			return def;
		}
		return t;
	}
}
